<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<table id="user_table">
    <thead>
    <th data-options="field:'id'">ID</th>
    <th data-options="field:'realName'">真实姓名</th>
    <th data-options="field:'email'">邮箱</th>
    <th data-options="field:'dept',formatter:deptFormatter">部门</th>
    <th data-options="field:'jobId',formatter:jobFormatter">职位</th>
    </thead>
</table>
<div id="user_tool">
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="doUserAdd()">添加</a>
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="doUserUpdate()">修改</a>
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="doRoleAssign()">分配角色</a>
</div>
<div id="user_dialog" class="easyui-dialog" title="编辑用户"
     data-options="modal:true,closed:true,href:'<%=request.getContextPath()%>/user.html?act=go_edit',footer:'#user_buttons'"
     style="width: 400px;height: 300px">
    s
</div>
<div id="user_role_dialog" class="easyui-dialog" title="分配角色"
     data-options="modal:true,closed:true,footer:'#user_role_buttons'" style="width: 400px;height: 300px">
    <ul id="user_role_tree" class="easyui-tree">

    </ul>
</div>
<div id="user_buttons" style="padding: 5px 15px 5px 0px;text-align: right">
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-ok'" onclick="submitUserForm()">提交</a>
    <a href="#" class="easy-linkbutton" onclick="doCancel('user_dialog')">取消</a>
</div>
<div id="user_role_buttons" style="padding: 5px 15px 5px 0px;text-align: right">
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-ok'" onclick="submitUserRole()">提交</a>
    <a href="#" class="easy-linkbutton" onclick="doCancel('user_role_dialog')">取消</a>
</div>
<script type="text/javascript">
    function deptFormatter(value,row,index) {
        return row.dept.name;
    }
    function jobFormatter(value,row,index) {
        switch (value){
            case 1:return "职员"
            case 2:return "项目经理"
            case 3:return "总监"
        }
    }
    $(function () {
        $("#user_table").datagrid({
            url:"<%=request.getContextPath()%>/user.html?act=list",
            singleSelect:true,
            toolbar:"#user_tool"
        })
    });

    function doUserAdd() {
        $("#user_dialog").dialog("open");
    }
    function doCancel(domId) {
        $("#"+domId).dialog("close")
    }

    function submitUserForm() {
        $.ajax({
            url:"<%=request.getContextPath()%>/user.html?act=edit",
            data:$("#user_form").serialize(),
            mothod:"post",
            success:function (data) {
                if (data.status){
                    $("#user_dialog").dialog("close");
                    $("#user_table").datagrid("load");
                }else {
                    alert(data.message);
                }
            }
        })
    }

    function doUserUpdate() {
        var row = $("#user_table").datagrid("getSelected");
        if (row == null || row.length == 0){
            return;
        }
        $("#user_dialog").dialog({
            onLoad:function () {
                $("#user_form").form("load",{
                    id:row.id,
                    realName:row.realName,
                    email:row.email,
                    status:row.status
                })
            }
        }).dialog("open");
    }

    function doRoleAssign() {
        var row = $("#user_table").datagrid("getSelected");
        if (row == null || row.length == 0){
            return;
        }
        $.ajax({
            url:"<%=request.getContextPath()%>/user.html?act=user_role",
            data:{
                userId:row.id
            },
            mothod:"post",
            success:function (roleIds) {
                $("#user_role_tree").tree({
                    url:"<%=request.getContextPath()%>/role.html?act=list",
                    mathod:"post",
                    checkbox:true,
                    formatter:function (node) {
                      return   node.remark+"["+node.name+"]"
                    },
                    onLoadSuccess:function (node,data) {
                        for(var i =0;i < roleIds.length;i++){
                            var nodeRole = $("#user_role_tree").tree("find",roleIds[i]);
                            $("#user_role_tree").tree("check",nodeRole.target);
                            /*if($("#user_role_tree").tree("isLeaf",nodeRole.target)){
                            }*///不存在层级关系，不用判断叶子节点
                        }
                        $("#user_role_dialog").dialog("open")
                    }
                })
            }
        })

    }
    function submitUserRole() {
        var row = $("#user_table").datagrid("getSelected");
        if (row == null || row.length == 0){
            return;
        }
        var roleIds = new Array();
        $($("#user_role_tree").tree("getChecked")).each(function (i,node) {
            roleIds[i] = node["id"]
        })
        $.ajax({
            url:"<%=request.getContextPath()%>/user.html?act=assign_role",
            data:{
                userId:row["id"],
                roleIds:roleIds.join(",")
            },
            mothod:"post",
            success:function (data) {
                if (data.status){
                    $("#user_role_dialog").dialog("close");
                }else {
                    alert(data.message);
                }
            }
        })
    }
</script>
